package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.thrd.basicframework.http.utils.StatusCode;
import com.thrd.taiexhibition.R;

/**
 * @author: ${唐杰}
 * @date: 2021/4/7
 * 缺省图
 */
public class DefaultView extends RelativeLayout implements View.OnClickListener {

    private ImageView mImage;
    private TextView mText;

    public DefaultView(Context context) {
        this(context,null);
    }

    public DefaultView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DefaultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view=View.inflate(context, R.layout.view_default,this);
        mImage = view.findViewById(R.id.iv_image);
        mText = view.findViewById(R.id.tv_text);
        mText.setOnClickListener(this);
        setVisibility(GONE);
        setEnabled(false);
    }

    public void error(String error, int type){

        this.animate().alpha(1).start();
        setEnabled(true);
        setVisibility(VISIBLE);
        switch (type){
            //数据不存在
            case 10000:
                mImage.setImageResource(R.mipmap.icon_ni_data);
                break;
            //服务器异常
            case StatusCode.NO_SERVICE_ERROR:
                mImage.setImageResource(R.mipmap.icon_service_error);
                break;
            //没有网络
            case StatusCode.NO_INTERNET:
                mImage.setImageResource(R.mipmap.icon_no_net);
                break;
            //没有数据
            default :
                mImage.setImageResource(R.mipmap.icon_ni_data);
//                mImage.setImageResource(R.mipmap.icon_no_net);
                break;

        }
    }

    public void success(){
        this.animate().alpha(0).setDuration(500).start();
        setVisibility(GONE);
        setEnabled(false);
    }

    @Override
    public void onClick(View v) {
        if(onRetryClick!=null){
            onRetryClick.onRetryClick();
        }
    }

    public OnRetryClick onRetryClick;

    public OnRetryClick getOnRetryClick() {
        return onRetryClick;
    }

    public void setOnRetryClick(OnRetryClick onRetryClick) {
        this.onRetryClick = onRetryClick;
    }

    public interface OnRetryClick{
        public  void onRetryClick();
    }

}
